package com.edianping.store;

import java.sql.Connection;
import java.sql.Statement;
import java.util.List;

import org.apache.log4j.Logger;

import com.edianping.store.db.ConnectionPool;
import com.edianping.util.LogCode;

public abstract class AbstractDBStore {
	protected static ConnectionPool pool = ConnectionPool.getInstance();
	protected static Logger log = Logger.getLogger(AbstractDBStore.class);
	
	public static  boolean ExecuteSQL(String strQuery){
		
		Connection conn = null;
		Statement stmt=null;

		try{
			conn = pool.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(strQuery);
			
			log.debug( strQuery );
			
			stmt.close();
		}catch(Exception  e){
			log.error("[["+LogCode.E_DB_SQL_ERROR+"]], SQL:["+ strQuery + "]");
			if (log.isDebugEnabled())
				log.debug(e);
			return false;
		}finally{
			if(conn!=null)
				pool.checkin(conn);
		}
		
		return true;
	}
	
	public static  boolean ExecuteMultiSQL( List<String> strQuery ){
		
		Connection conn = null;
		Statement stmt = null;

		try{
			conn = pool.getConnection();
			stmt = conn.createStatement();
			
			for( String str : strQuery ){
				stmt.executeUpdate( str );
			}
			
			stmt.close();
		}catch(Exception  e){
			log.error("[["+LogCode.E_DB_SQL_ERROR+"]], SQL:["+ strQuery + "]");
			if (log.isDebugEnabled())
				log.debug(e);
			return false;
		}finally{
			if(conn!=null)
				pool.checkin(conn);
		}
		
		return true;
	}
}
